{{>partial_header}}

using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using UnityEngine.Networking;

namespace {{packageName}}.Client
{
    /// <summary>
    /// Provides an awaiter for UnityWebRequestAsyncOperation to allow `await request.SendWebRequest()`
    /// in Unity async methods. Unity's AsyncOperation types are not awaitable by default, so this
    /// extension bridges that gap by converting the operation into a Task.
    /// </summary>
    public static class UnityWebRequestAwaiterExtensions
    {
        public static TaskAwaiter<UnityWebRequestAsyncOperation> GetAwaiter(this UnityWebRequestAsyncOperation asyncOp)
        {
            var tcs = new TaskCompletionSource<UnityWebRequestAsyncOperation>();
            asyncOp.completed += _ => tcs.SetResult(asyncOp);
            return tcs.Task.GetAwaiter();
        }
    }
}
